<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <meta charset=utf-8>
    <meta content="IE=edge" http-equiv=X-UA-Compatible>
    <meta content="width=device-width,initial-scale=1" name=viewport>
    <title>
        js调用java
    </title>
</head>

<body>
<p>
    <xmp id="show">
    </xmp>
</p>
<p>
    <xmp id="init">
    </xmp>
</p>
<p>
    <input type="text" id="text1" value="用户名(username)"/>
</p>
<p>
    <input type="text" id="text2" value="password"/>
</p>
<p>
    <input type="button" id="enter" value="发消息给Native" onclick="testClick();"
    />
</p>

<p>
    <input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"
    />
</p>

<p>
    <input type="button" id="enter2" value="显示html" onclick="testDiv();"/>
</p>

<p>
<form action="demo_form.asp">
    选择图片:<input type="file" value="选择图片" name="图片" accept="image/*"/>
    <input type="submit">
</form>
</p>

<p>
<form>
    选择图片2:<input type="file" accept="image/*"/>
    <input type="submit">
</form>
</p>

<p>
    打开文件:<input type="file" value="打开文件" name="文件" accept="*"/>
</p>

</body>
<script>
        function testDiv() {
            document.getElementById("show").innerHTML = document.getElementsByTagName("html")[0].innerHTML;
        }

        function testClick() {
            var str1 = document.getElementById("text1").value;
            var str2 = document.getElementById("text2").value;

            //send message to native
            var data = {id: 1, content: "这是一个图片 <img src=\"a.png\"/> test\r\nhahaha"};

            console.log(window.androidJs, data);

            window.androidJs.send(
                data
                , function(responseData) {
                    document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData
                }
            );

        }

        function testClick1() {
            var str1 = document.getElementById("text1").value;
            var str2 = document.getElementById("text2").value;

            //call native method
            window.androidJs.callHandler(
                'submitFromWeb'
                , {'param': str1 + "\n" + str2 + "\n" + '中文测试:'+ Date()}
                , function(responseData) {
                    document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
                }
            );
        }

        function bridgeLog(logContent) {
            document.getElementById("show").innerHTML = logContent;
        }

        function connectWebViewJavascriptBridge(callback) {
            if (window.androidJs) {
                callback(androidJs)
            } else {
                document.addEventListener(
                    'WebViewJavascriptBridgeReady'
                    , function() {
                        callback(androidJs)
                    },
                    false
                );
            }
        }

        connectWebViewJavascriptBridge(function(bridge) {
            bridge.init(function(message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': '测试中文!'
                };

                if (responseCallback) {
                    console.log('JS responding with', data);
                    responseCallback(data);
                }
            });

            bridge.registerHandler("functionInJs", function(data, responseCallback) {
                document.getElementById("show").innerHTML = ("data from Java: = " + data);
                if (responseCallback) {
                    var responseData = "Javascript Says Right back aka!";
                    responseCallback(responseData);
                }
            });
        })


















</script>

</html>

